package com.xmall.pethouse_api.controller;

import com.xmall.pethouse_api.pojo.Result;
import com.xmall.pethouse_api.pojo.entity.Pet;
import com.xmall.pethouse_api.pojo.dto.AddPetDto;
import com.xmall.pethouse_api.service.PetService;
import com.xmall.pethouse_api.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pets")
public class PetController {

    @Autowired
    private PetService petService;

    // 查询当前登录用户的所有宠物
    @GetMapping("/list")
    public Result<List<Pet>> getMyPets() {
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Long userId = Long.valueOf(userMap.get("id").toString());
        List<Pet> pets = petService.findByOwnerId(userId);
        return Result.success(pets);
    }
    
    // 添加宠物
    @PostMapping("/add")
    public Result<Pet> addPet(@RequestBody AddPetDto addPetDto) {
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Long userId = Long.valueOf(userMap.get("id").toString());
        Pet pet = petService.addPet(addPetDto, userId);
        return Result.success(pet);
    }
} 